package com.njust.diary.service;

import cn.hutool.core.util.StrUtil;
import com.njust.diary.dao.HealthDao;
import com.njust.diary.po.Health;
import com.njust.diary.po.NoteType;
import com.njust.diary.po.User;
import com.njust.diary.vo.Result;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


public class HealthService {
private HealthDao healthDao = new HealthDao();

    public Health findHealth(Integer userId) {
        Health health = healthDao.findHealthByUserId(userId);
        return  health;


    }

    public Result<Integer> updateHealth(Integer userId, Integer age, Integer gender, Integer weight, Integer height, HttpServletRequest req,Integer xue1,Integer xue2,Integer xue3,Integer xin1,Integer xin2,Integer xin3,Integer yi1,Integer yi2,Integer yi3,Integer jiao1,Integer jiao2) {

        Integer code= healthDao.checkUserId(userId);

        Result<Integer> result = new Result<>();
       if(code==1) {//没有数据，插入操作
           Health health =new Health();
           health.setUserId(userId);
           health.setHeight(height);
           health.setAge(age);
           health.setGender(gender);
           health.setWeight(weight);
           health.setXue1(xue1);
           health.setXue2(xue2);
           health.setXue3(xue3);
           health.setXin1(xin1);
           health.setXin2(xin2);
           health.setXin3(xin3);
           health.setYi1(yi1);
           health.setYi2(yi2);
           health.setYi3(yi3);
           health.setJiao1(jiao1);
           health.setJiao2(jiao2);

           int row =healthDao.insert(health);
           if(row>0){
               result.setCode(1);
               result.setMsg("插入信息成功");
           }else{
               result.setCode(0);
               req.getSession().setAttribute("health",health);
               result.setMsg("插入信息失败");
           }
       }else{//有数据，更新操作
           Health health =new Health();
           health.setUserId(userId);
           health.setHeight(height);
           health.setAge(age);
           health.setGender(gender);
           health.setWeight(weight);
           health.setXue1(xue1);
           health.setXue2(xue2);
           health.setXue3(xue3);
           health.setXin1(xin1);
           health.setXin2(xin2);
           health.setXin3(xin3);
           health.setYi1(yi1);
           health.setYi2(yi2);
           health.setYi3(yi3);
           health.setJiao1(jiao1);
           health.setJiao2(jiao2);

           int row =healthDao.update(health);
           if(row>0){
               result.setCode(1);
               req.getSession().setAttribute("health",health);
               result.setMsg("更新信息成功");
           }else{
               result.setCode(0);
               result.setMsg("更新信息失败");
           }


       }

    return  result;
    }
}
